package com.example.jay10_8;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;


// import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgAddress;
    private ImageButton mImgSettings;

    @SuppressLint("StaticFieldLeak")
    private static MainActivity myMainActivity;

    public static final String CTL_ACTION =
            "org.xr.action.CTL_ACTION";
    public static final String UPDATE_ACTION =
            "org.xr.action.UPDATE_ACTION";

    private Fragment mTab01 = new WeixinFragment();
    private Fragment mTab02 = new FrdFragment();
    private Fragment mTab03 = new AddressFragment();
    private Fragment mTab04 = new SettingFragment();

    ActivityReceiver activityReceiver;


    // 获取界面中显示歌曲标题、作者文本框
    TextView title, author;
    // 播放/暂停、停止按钮
    ImageButton play, stop;

    // 定义音乐的播放状态，0x11代表没有播放；0x12代表正在播放；0x13代表暂停
    int status = 0x11;
    String[] titleStrs = new String[] { "Legends Never Die", "约定", "美丽新世界" };
    String[] authorStrs = new String[] { "英雄联盟", "周蕙", "伍佰" };



    private FragmentManager fm;

    public static MainActivity getmInstanceActivity() {
        return myMainActivity;
    }

    View.OnClickListener onClickListener1 = new View.OnClickListener() {
        @Override
        public void onClick(View source) {
            // 创建Intent
            Intent intent = new Intent("org.xr.action.CTL_ACTION");
            switch (source.getId()) {
                // 按下播放/暂停按钮
                case R.id.play:
                    intent.putExtra("control", 1);
                    break;
                // 按下停止按钮
                case R.id.stop:
                    intent.putExtra("control", 2);
                    break;
            }
            // 发送广播，将被Service组件中的BroadcastReceiver接收到
            MainActivity.getmInstanceActivity().sendBroadcast(intent);
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 主函数
        myMainActivity = this;
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
        initEvent();
        selectfragment(0);

        // 获取程序界面界面中的两个按钮
        play = (ImageButton) this.findViewById(R.id.play);
        stop = (ImageButton) this.findViewById(R.id.stop);
        title = (TextView) this.findViewById(R.id.title);
        author = (TextView) this.findViewById(R.id.author);

        // 为两个按钮的单击事件添加监听器
        play.setOnClickListener(this.onClickListener1);
        stop.setOnClickListener(this.onClickListener1);


        activityReceiver = new ActivityReceiver();

        // 创建IntentFilter
        IntentFilter filter = new IntentFilter();
        // 指定BroadcastReceiver监听的Action
        filter.addAction(UPDATE_ACTION);
        // 注册BroadcastReceiver
        registerReceiver(activityReceiver, filter);

        Intent intent = new Intent(this, MusicService.class);
        // 启动后台Service
        startService(intent);


    }


    // 自定义的BroadcastReceiver，负责监听从Service传回来的广播
    public class ActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取Intent中的update消息，update代表播放状态
            int update = intent.getIntExtra("update", -1);
            // 获取Intent中的current消息，current代表当前正在播放的歌曲
            int current = intent.getIntExtra("current", -1);
            if (current >= 0) {
                title.setText(titleStrs[current]);
                author.setText(authorStrs[current]);
            }


            switch (update) {
                case 0x11:
                    play.setImageResource(R.drawable.play);
                    status = 0x11;
                    break;
                // 控制系统进入播放状态
                case 0x12:
                    // 播放状态下设置使用暂停图标
                    play.setImageResource(R.drawable.pause);
                    // 设置当前状态
                    status = 0x12;
                    break;
                // 控制系统进入暂停状态
                case 0x13:
                    // 暂停状态下设置使用播放图标
                    play.setImageResource(R.drawable.play);
                    // 设置当前状态
                    status = 0x13;
                    break;
            }
        }
    }

    private void initFragment() {
        /* 在FrameLayout中添加按钮  */

        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        transaction.add(R.id.id_content,mTab01);
        transaction.add(R.id.id_content,mTab02);
        transaction.add(R.id.id_content,mTab03);
        transaction.add(R.id.id_content,mTab04);
        transaction.commit();

    }

    private void initView(){
        //LinearLayout mTabWeixin = (LinearLayout) findViewById(R.id.weixin_tab1);
        //LinearLayout mTabFrd = (LinearLayout) findViewById(R.id.friend_tab2);
        //LinearLayout mTabAddress = (LinearLayout) findViewById(R.id.address_tab3);
        //LinearLayout mTabSettings = (LinearLayout) findViewById(R.id.setting_tab4);

        /* 将button设置为对应的ImageButton类型的对象   */

        mImgWeixin = (ImageButton) findViewById(R.id.weixin_image_button);
        mImgFrd = (ImageButton) findViewById(R.id.friend_image_button);
        mImgAddress = (ImageButton) findViewById(R.id.address_image_button);
        mImgSettings = (ImageButton) findViewById(R.id.setting_image_button);

    }

    private void initEvent(){
        /*
          设置点击监听程序 (设置图片监听为例)
          */
        mImgWeixin.setOnClickListener(onClickListener2);
        mImgFrd.setOnClickListener(onClickListener2);
        mImgAddress.setOnClickListener(onClickListener2);
        mImgSettings.setOnClickListener(onClickListener2);
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

    private void selectfragment(int i){
        /*
         * 执行此函数时,将灰色图标设置为其对应的彩色图标。
         * */

        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgWeixin.setImageResource(R.drawable.jjg);
                break;
            case 1:
                transaction.show(mTab02);
                mImgFrd.setImageResource(R.drawable.jjg);
                break;
            case 2:
                transaction.show(mTab03);
                mImgAddress.setImageResource(R.drawable.jjg);
                break;
            case 3:
                transaction.show(mTab04);
                mImgSettings.setImageResource(R.drawable.jjg);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    View.OnClickListener onClickListener2 = new View.OnClickListener() {
        @Override
        public void onClick(View v){
            /*
             * 设置点击按钮动作监听,如果点击到对应按钮,执行对应的selectfragment函数,并将其他图标reset为灰色。
             * */
            resetimg();
            switch (v.getId()){
                case R.id.weixin_image_button:
                    selectfragment(0);
                    break;

                case R.id.friend_image_button:
                    selectfragment(1);
                    break;

                case R.id.address_image_button:
                    selectfragment(2);
                    break;

                case R.id.setting_image_button:
                    selectfragment(3);
                    break;

                default:
                    break;
            }
        }
    };

    private void resetimg() {
        /*
         * 切换图片至暗色.
         * */
        mImgWeixin.setImageResource(R.drawable.weixin_normal);
        mImgFrd.setImageResource(R.drawable.friend_normal);
        mImgAddress.setImageResource(R.drawable.address_normal);
        mImgSettings.setImageResource(R.drawable.setting_normal);
    }

}